<template>
  <div>
    <!--按钮组-->
    <div style="display: flex;justify-content: space-between">
      <el-button @click="showAddSalaryView" type="primary" icon="el-icon-plus">添加工资账套</el-button>
      <el-button @click="initSalaries" type="success" icon="el-icon-refresh"></el-button>
    </div>

    <!--表格-->
    <div style="margin-top: 10px">
      <el-table
          v-loading="loading"
          element-loading-text="拼命加载中..."
          border
          stripe
          :data="salaries">
        <el-table-column
            type="selection"
            width="40">
        </el-table-column>
        <el-table-column
            width="120"
            label="账套名称"
            prop="name">
        </el-table-column>
        <el-table-column
            width="70"
            label="基本工资"
            prop="basicSalary">
        </el-table-column>
        <el-table-column
            width="70"
            label="交通补助"
            prop="trafficSalary">
        </el-table-column>
        <el-table-column
            width="70"
            label="午餐补助"
            prop="lunchSalary">
        </el-table-column>
        <el-table-column
            width="70"
            label="奖金"
            prop="bonus">
        </el-table-column>
        <el-table-column
            width="120"
            label="启用时间"
            prop="createDate">
        </el-table-column>
        <el-table-column
            align="center"
            label="养老金">
          <el-table-column prop="pensionPer"
                           width="70"
                           label="比率">
          </el-table-column>
          <el-table-column prop="pensionBase"
                           width="70"
                           label="基数">
          </el-table-column>
        </el-table-column>
        <el-table-column
            align="center"
            label="医疗保险">
          <el-table-column prop="medicalPer"
                           width="70"
                           label="比率">
          </el-table-column>
          <el-table-column prop="medicalBase"
                           width="70"
                           label="基数">
          </el-table-column>
        </el-table-column>
        <el-table-column
            align="center"
            label="公积金">
          <el-table-column prop="accumulationFundPer"
                           width="70"
                           label="比率">
          </el-table-column>
          <el-table-column prop="accumulationFundBase"
                           width="70"
                           label="基数">
          </el-table-column>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="showEditSalaryView(scope.row)" type="primary">编辑</el-button>
            <el-button @click="onDeleteSalary(scope.row)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--对话框-->
    <el-dialog
        :title="dialogTitle"
        :visible.sync="dialogVisible"
        width="50%">
      <div style="display: flex;justify-content: space-around;align-items: center">
        <!--步骤条-->
        <el-steps direction="vertical" :active="activeIndex">
          <el-step v-for="(item, index) in salaryItemName" :key="index" :title="item"></el-step>
        </el-steps>
        <!--输入框-->
        <el-input v-model="salary[title]" style="width: 200px;" v-show="activeIndex == index" :placeholder="'请输入' + salaryItemName[index] + '...'" v-for="(value, title, index) in salary" :key="index"></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="preStep">{{ activeIndex==10? '取消':'上一步'}}</el-button>
        <el-button type="primary" @click="nextStep">{{ activeIndex==10 ? '完成':'下一步'}}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "SalSob",
  data(){
    return {
      loading: false,
      dialogTitle: '添加工资账套',
      salaries: [],
      dialogVisible: false,
      activeIndex: 0,
      salaryItemName: [
          '账套名称',
          '基本工资',
          '交通补助',
          '午餐补助',
          '奖金',
          '养老金比率',
          '养老金基数',
          '医疗保险比率',
          '医疗保险基数',
          '公积金比率',
          '公积金基数',
      ],
      salary: {
        name: '',
        basicSalary: 0,
        trafficSalary: 0,
        lunchSalary: 0,
        bonus: 0,
        pensionPer: 0,
        pensionBase: 0,
        medicalPer: 0,
        medicalBase: 0,
        accumulationFundPer: 0,
        accumulationFundBase: 0,
      }
    }
  },
  methods: {
    //初始化数据
    initSalaries(){
      this.loading = true
      this.getRequest('/salary/sob/').then(res => {
        this.loading = false
        if (res){
          this.salaries = res
        }
      })
    },
    //显示修改对话框复用形式
    showEditSalaryView(row){
      this.dialogTitle = '编辑工资账套'
      this.activeIndex = 0
      //赋值属性，顺序要对应
      this.salary.id = row.id
      this.salary.name = row.name
      this.salary.basicSalary = row.basicSalary
      this.salary.trafficSalary = row.trafficSalary
      this.salary.lunchSalary = row.lunchSalary
      this.salary.bonus = row.bonus
      this.salary.pensionPer = row.pensionPer
      this.salary.pensionBase = row.pensionBase
      this.salary.medicalPer = row.medicalPer
      this.salary.medicalBase = row.medicalBase
      this.salary.accumulationFundPer = row.accumulationFundPer
      this.salary.accumulationFundBase = row.accumulationFundBase
      //显示对话框
      this.dialogVisible = true
    },
    //显示添加对话框
    showAddSalaryView(){
      this.dialogTitle = '添加工资账套'
      //情空之前的数据，并把步骤置为0
      this.salary = {
        name: '',
        basicSalary: 0,
        trafficSalary: 0,
        lunchSalary: 0,
        bonus: 0,
        pensionPer: 0,
        pensionBase: 0,
        medicalPer: 0,
        medicalBase: 0,
        accumulationFundPer: 0,
        accumulationFundBase: 0,
      }
      this.dialogVisible = true
      this.activeIndex = 0
    },
    //删除事件
    onDeleteSalary(row){
      this.$confirm('此操作将永久删除：'+ row.name +', 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/salary/sob/' + row.id).then(res => {
          if (res){
            //删除后刷新数据
            this.initSalaries()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    //下一步
    nextStep(){
      if (this.activeIndex == 10){
        //存在id 为修改，不存在为添加
        if (this.salary.id){
          this.putRequest('/salary/sob/', this.salary).then(res => {
            if (res){
              //刷新数据，关闭对话框
              this.initSalaries()
              this.dialogVisible = false
            }
          })
        }else {
          //添加
          this.postRequest('/salary/sob/', this.salary).then(res => {
            if (res){
              //初始化数据
              this.initSalaries()
              this.dialogVisible = false
            }
          })
        }
        return
      }
      this.activeIndex++
    },
    //上一步
    preStep(){
      if (this.activeIndex == 0){
        return
      }else if (this.activeIndex == 10){
        this.dialogVisible = false
        return
      }
      this.activeIndex--
    },
  },
  mounted() {
    this.initSalaries()
  }
}
</script>

<style scoped>

</style>
